﻿// -----------------------------------------------------------------------
// <copyright file="ImplementationListData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db ImplementationList
    /// </summary>
    public class ImplementationListData
    {
        /// <summary>
        /// Get ImplementationList By Id
        /// </summary>
        /// <param name="implementationListId">Param implementationListId</param>
        /// <returns>Return ImplementationList</returns>
        public ImplementationListEN GetImplementationListById(int implementationListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ImplementationListEN implementationList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneImplementationListGetById, implementationListId);
                while (reader.Read())
                {
                    implementationList = new ImplementationListEN();

                    if (!reader.IsDBNull(0))
                    {
                        implementationList.ImplementationListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        implementationList.Name = reader.GetString(1);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return implementationList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all ImplementationList
        /// </summary>
        /// <returns>Return list all ImplementationList</returns>
        public IList<ImplementationListEN> GetImplementationLists()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ImplementationListEN> lstImplementationList = new List<ImplementationListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneImplementationListGetAll);
                while (reader.Read())
                {
                    ImplementationListEN implementationList = new ImplementationListEN();

                    if (!reader.IsDBNull(0))
                    {
                        implementationList.ImplementationListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        implementationList.Name = reader.GetString(1);
                    }

                    lstImplementationList.Add(implementationList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstImplementationList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db ImplementationList
        /// </summary>
        /// <param name="implementationList">Paarm ImplementationList</param>
        /// <returns>Insert ImplementationList</returns>
        public int Insert(ImplementationListEN implementationList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ImplementationList.Name, implementationList.Name)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneImplementationListInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db ImplementationList
        /// </summary>
        /// <param name="implementationList">Param ImplementationList</param>
        public void Update(ImplementationListEN implementationList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ImplementationList.ImplementationListId, implementationList.ImplementationListId),
                                    new SqlParameter(Constants.SqlParamerterName.ImplementationList.Name, implementationList.Name)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneImplementationListUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete ImplementationList by implementationListId
        /// </summary>
        /// <param name="implementationListId">param implementationListId</param>
        public void Delete(int implementationListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ImplementationList.ImplementationListId, implementationListId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneImplementationListDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}